#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<unordered_map>
#include"log.hpp"
#include"Common.hpp"
using namespace LogModule;
using namespace std;

const string gpath = "./"; //字典所在路径
const string gdictname = "dict.txt";
const string gsep = ":";//分割符

class Dict
{
private:
  bool LoadDictionary()//加载字典
  {
     string file = _path + _filename;
     ifstream in(file.c_str());
     if(!in.is_open())  //打开不成功
     {
        LOG(LogLevel::ERROR) << "open file" << file << "error\n" ;
        return false;
     }
     //按行为单位读取 banana: 香蕉
     string line;
     while(getline(in,line))
     {
        //切分成左右部分
        string key; //左边
        string value;//右边
        if(SplitString(line,&key,&value,gsep))//切分成功 失败不处理继续下一行
        { 
            _dictionary.insert(make_pair(key,value));  
        }
     }
     in.close();//关闭
     return true;
  }


public:
  Dict(const string& path = gpath,const string& filename = gdictname)
  :_path(path),_filename(filename)
  {
      LoadDictionary();
  }

  

  string Translate(const string& word)//根据字典翻译
  {
      auto iter = _dictionary.find(word);
      if(iter == _dictionary.end()) return "None";//找不到
      //找到
      return iter->second;
  }

  ~Dict()
  {}

 private:
   unordered_map<string,string> _dictionary; 
   string _path; //字典路径
   string _filename;//字典名
};








